//
//  AZURLRequest+Configure.m
//  FKNetwork
//
//  Created by G-Xi0N on 2018/4/3.
//  Copyright © 2018年 gaoX. All rights reserved.
//

#import "AZURLRequest+Configure.h"
#import "AZBaseModel+Map.h"
#import "FKResponse.h"

#define kBaseURL [NSURL URLWithString:@"https://api.github.com/"]

@implementation AZURLRequest (Configure)

+ (instancetype)requestWithPath:(NSString *)path parameters:(NSDictionary *)parameters
{
    AZURLRequest *request = [self requestWithBaseURL:kBaseURL path:path parameters:parameters];
    request.requestSerializer.timeoutInterval = 10;
    request.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", @"image/jpeg", @"image/png", nil];
    return request;
}

- (void)completionWithSuccess:(void (^)(FKResponse *))success failure:(void (^)(NSError *))failure
{
    [self completionWithCache:nil success:success failure:failure];
}

- (void)completionWithCache:(void (^)(FKResponse *))cache
                    success:(void (^)(FKResponse *))success
                    failure:(void (^)(NSError *))failure
{
    [self startWithCache:^(id cachedData) {
        FKResponse *response = [FKResponse mapObject:cachedData];
        !cache ?: cache(response);
    } success:^(id object) {
        FKResponse *response = [FKResponse mapObject:object];
        !success ?: success(response);
    } failure:^(NSError *error) {
        !failure ?: failure(error);
    }];
}

@end
